package com.yxzz.controller;

import com.ruoyi.common.core.constant.TokenConstants;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.model.LoginWxUser;
import com.yxzz.core.domain.PPatient;
import com.yxzz.core.domain.TUserPatientRela;
import com.yxzz.core.service.IPPatientService;
import com.yxzz.core.service.ITUserPatientRelaService;
import com.yxzz.service.UserPatientRelationshipService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 病人信息Controller
 *
 * @author ruoyi
 * @date 2023-02-28
 */
@RestController
@RequestMapping("/patient")
public class PPatientController extends BaseController
{
    @Autowired
    private IPPatientService pPatientService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private UserPatientRelationshipService userPatientRelationshipService;

    /**
     * 查询病人信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(HttpServletRequest request)
    {
        // 从header获取token标识
        String token = SecurityUtils.getToken(request);
        LoginWxUser loginWxUser = tokenService.getLoginWxUser(token);
        List<PPatient> list = userPatientRelationshipService.getPatientByUserId(loginWxUser);
        return getDataTable(list);
    }


    /**
     * 获取病人信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pPatientService.selectPPatientById(id));
    }

    /**
     * 新增病人信息
     */
    @Log(title = "病人信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(HttpServletRequest request, @RequestBody PPatient pPatient)
    {
        String token = SecurityUtils.getToken(request);
        LoginWxUser loginWxUser = tokenService.getLoginWxUser(token);
        userPatientRelationshipService.addPatient(pPatient, loginWxUser);
        return toAjax(pPatientService.insertPPatient(pPatient));
    }

    /**
     * 修改病人信息
     */
    @Log(title = "病人信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PPatient pPatient)
    {
        return toAjax(pPatientService.updatePPatient(pPatient));
    }

    /**
     * 删除病人信息
     */
    @Log(title = "病人信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pPatientService.deletePPatientByIds(ids));
    }
}
